<template>
  <div class="edit-box">
    <el-dialog width="600px" :title="dialogTitle" v-model="dialogFormVisible" :close-on-click-modal="false" :draggable="true"
      @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="员工姓名" prop="name">
                <el-input v-model="dialogForm.name" placeholder="请输入员工姓名" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="手机号码（登录账号）" prop="phone">
                <el-input v-model="dialogForm.phone" maxlength="11" placeholder="请输入手机号码" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="岗位" prop="roleId">
                <el-select v-if="!isShowName" style="width:100%;" v-model="dialogForm.roleId" placeholder="请选择岗位">
                  <el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
                </el-select>
                <el-input v-else v-model="dialogForm.roleName" disabled />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="员工状态" prop="state">
                <el-radio-group :disabled="isShowName" v-model="dialogForm.state">
                  <el-radio :label="1">启用</el-radio>
                  <el-radio :label="0">禁用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-link v-if="!isAdd && !showPass" style="font-size:14px" type="primary" @click="showPass = true">重置密码</el-link>
              <el-form-item v-if="isAdd || showPass" style="width:90%;" label="设置密码" prop="firstPassword">
                <el-input v-model="dialogForm.firstPassword" placeholder="请输入员工登录密码" type="password" :show-password="isAdd"
                  @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="isAdd || showPass" style="width:90%;" label="确认密码" prop="secondPassword">
                <el-input v-model="dialogForm.secondPassword" placeholder="请输入确认密码" type="password" :show-password="isAdd"
                  @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-divider />
          <el-row>
            <el-col :span="24">
              <el-form-item label="登录密码" prop="password">
                <el-input v-model="dialogForm.password" placeholder="为保证本次操作安全，请输入登录密码" type="password" show-password
                  @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row> -->
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import api from '../api'
import { warningDeal } from '@/utils/utils'
import hexMD5 from "@/utils/md5.min.js"
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogTitle: '',
      isShowName: false,
      dialogFormVisible: false,
      isAdd: true,
      showPass: false,
      dialogForm: {
        state: 1,
      },
      roleOptions: [],
      dialogFormRules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '手机号码格式不正确', trigger: 'blur'
          }
        ],
        roleId: [
          { required: true, message: '请选择岗位', trigger: 'blur' },
        ],
        state: [
          { required: true, message: '请选择状态', trigger: 'blur' },
        ],
        firstPassword: [
          { required: true, message: '请输入员工登录密码', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{6,}$/, message: '请输入6位以上且只包含数字、字母的密码', trigger: 'blur'
          },
        ],
        secondPassword: [
          { required: true, message: '请输入确认登录密码', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{6,}$/, message: '请输入6位以上且只包含数字、字母的密码', trigger: 'blur'
          },
        ],
        password: [
          { required: true, message: '为保证本次操作安全，请输入登录密码', trigger: 'blur' },
        ],
      },
    })

    function dialogShow(data = null) {
      state.dialogTitle = '新建员工'
      if (data) {
        state.dialogTitle = '编辑员工信息'
        state.dialogForm = { ...data }
        state.isAdd = false
      }
      getRoleList()
      state.dialogFormVisible = true
    }

    function checkId(id) {
      let is = true
      state.roleOptions.map(item => {
        if (item.roleId == id) {
          is = false
        }
      })
      return is
    }

    function getRoleList() {
      api.roleRoles().then(res => {
        if (res.code == '200') {
          state.roleOptions = res.data.list
          if (state.dialogForm && state.dialogForm.roleId) {
            state.isShowName = checkId(state.dialogForm.roleId)
          }
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (state.isAdd) {
            const myObj = {
              firstPassword: hexMD5(state.dialogForm.firstPassword),
              secondPassword: hexMD5(state.dialogForm.secondPassword),
              name: state.dialogForm.name,
              phone: state.dialogForm.phone,
              roleId: state.dialogForm.roleId,
              state: state.dialogForm.state,
            }
            api.userAdd(myObj).then(res => {
              if (res.code == '200') {
                context.emit('dialogClose', true)
                state.dialogFormVisible = false
              } else {
                warningDeal(res.msg)
              }
            }).catch(err => {
              console.log(err)
            })
          } else {
            let myObj = {}
            if (state.dialogForm && state.dialogForm.firstPassword) {
              myObj = {
                userId: state.dialogForm.userId,
                password: hexMD5(state.dialogForm.firstPassword),
                name: state.dialogForm.name,
                phone: state.dialogForm.phone,
                roleId: state.dialogForm.roleId,
                state: state.dialogForm.state,
              }
            } else {
              myObj = {
                userId: state.dialogForm.userId,
                name: state.dialogForm.name,
                phone: state.dialogForm.phone,
                roleId: state.dialogForm.roleId,
                state: state.dialogForm.state,
              }
            }
            api.userModify(myObj).then(res => {
              if (res.code == '200') {
                context.emit('dialogClose', true)
                state.dialogFormVisible = false
              } else {
                warningDeal(res.msg)
              }
            }).catch(err => {
              console.log(err)
            })
          }
        }
      })
    }

    function dialogClose() {
      state.isAdd = true
      state.showPass = false
      state.isShowName = false
      state.dialogForm = {
        state: 1,
      }
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogShow,
      dialogClose,
      submitHandle,
    }
  }
}
</script>

<style>
</style>